package com.entities.projectversion.detail;



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.core.protocols.ICustomDatasource;
import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IDescriptionViewDelegate;
import com.core.protocols.IDetailTableViewDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.ui.controller.base.HibernateDetailController;
import com.core.utils.LocalizableString;
import com.entities.projectversion.servicelayer.VersionListLayer;
import com.model.project.Version;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;


public class VersionDetailController
        extends HibernateDetailController
        implements IDetailTableViewDelegate, IDescriptionViewDelegate, IServiceLayerDelegate, ICustomDatasource {



	private Version versionItem;



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	@Override
	public void reloadData () {

		Integer result = null;
		try {
			result = Integer.parseInt(this.versionId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		versionItem = (Version) versionListLayer.objectByUNID(result, null, this);
	}



	private VersionListLayer versionListLayer;
	private String           versionId;



	public String getVersionId () {

		return versionId;
	}



	public void setVersionId ( String versionId ) {

		this.versionId = versionId;
	}



	public VersionDetailController() {

		versionListLayer = new VersionListLayer();


	}



	@Override
	public String descriptionData () {

		// TODO Auto-generated method stub
		return null;
	}

	private String versionCreatedCell (Version versionItem, boolean readOnly ) {

		String result = null;
		String propertyValue = null;
		Date createDate = null;
		if (versionItem != null) {
			createDate = versionItem.getReleaseDate();
		}

		if (createDate == null) {
			createDate = new Date();
		}

		try {
			final DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
			final Calendar cal = dateFormat.getCalendar();
			cal.setLenient(false);
			dateFormat.setCalendar(cal);
			propertyValue = dateFormat.format(createDate);

		} catch (ParseException e) {

		}

		String caption = LocalizableString.sharedInstance().localizable("entity.Version.field.date");
		result = this.inputControl("date", caption, propertyValue, readOnly);
		return result;
	}

	@Override
	public int detailTableViewItemsCount ( String userInfo ) {

		int result = 0;
		String userInfoUC = userInfo.toUpperCase();
		if ( userInfoUC.equals("BASE") ) {
			result = 3;
		}
		return result;
	}



	@Override
	public String detailTableViewItemAtIndex ( String userInfo,
	                                           int index ) {

		String result = null;

		if ( userInfo.toUpperCase().equals("BASE") ) {
			result = this.versionBaseItemAtIndex(index);
		}
		return result;
	}



	private String versionBaseItemAtIndex ( int index ) {

		String result = null;

		String style = null;
		String propertyValue = null;
		String data = null;

		boolean readOnly = this.isReadOnly();

		switch ( index ) {
			case 0: {
				style = "-single";
				if ( versionItem != null ) {
					propertyValue = versionItem.getName();
				}
				String caption = LocalizableString.sharedInstance().localizable("entity.Version.field.name");
				data = this.inputControl("name", caption, propertyValue, readOnly);
				break;
			}
			case 1: {
				style = "-single";
				readOnly = this.isReadOnly();
				data = this.versionCreatedCell(this.versionItem, readOnly);
				break;
			}
			case 2: {
				style = "-single";
				if ( versionItem != null ) {
					propertyValue = versionItem.getDescription();
				}
				String caption = LocalizableString.sharedInstance().localizable("entity.Version.field.description");
				data = this.textAreaControl("description", caption, propertyValue, readOnly);
				break;
			}
			default: {
				break;
			}
		}
		result = String.format("<li class=\"property-item%s\"><div class=\"wrap\">%s</li>", style, data);
		return result;
	}
}
